<?php

abstract class MediaHandler
{
	private $_isError = false;
	private $_errorMessage = '';
	protected $_filePath = '';
	private $_libs;
	
	protected $_fileSize = 0;
	protected $_fileLength = 0;
	protected $_fileThumbNail = false;
	
	protected $_destPath = false;
	
	private $_convertStatus = MatchMediaStatus::NotProcessed;
	
	public function __construct($filePath)
	{
		$this->Init($filePath);
	}
	
	protected function Libs()
	{
    	if (!$this->_libs)	$this->_libs = SysLoader::GetLibCollection(); 
    	return $this->_libs; 
	}	

	protected function SetError($message)
	{
		$this->_errorMessage = $message;
		$this->_isError = true;
	}	
	
	public function IsError()
	{
		return $this->_isError;
	}

	public function GetErrorMessage()
	{
		return $this->_errorMessage;
	}	
	
	protected function Init($filePath, $isDelOldFile = true)
	{
		if (!is_file($filePath)) 
		{
			throw new Exception('Файл не найден');
		}		
		
		if ($isDelOldFile && $this->_filePath) 
		{
			if ($this->_filePath != $filePath) @unlink($this->_filePath);
			else clearstatcache();
		}
			
		$this->_filePath = $filePath;
		$this->_fileSize = $this->_fileLength = 0;
		$this->_fileThumbNail = false;
		$this->_convertStatus = MatchMediaStatus::NotProcessed;

		$this->InitFile();
		if ($this->IsError())
		{
			throw new Exception($this->GetErrorMessage());
		}
	}
	
	public function GetFilePath()
	{
		return $this->_filePath;
	}
	
	public function GetFileSize()
	{
		return $this->_fileSize;
	}
	
	public function GetFileLength()
	{
		return $this->_fileLength;	
	}	
	
	public function GetConvertStatus()
	{
		return $this->_convertStatus;
	}	
	
	protected function SetConvertStatus($status)
	{
		$this->_convertStatus = $status;
	}
	
	public function SetDestPath($destPath)
	{
		$this->_destPath = rtrim($destPath, '/'). '/';
	}	
	
	protected function GetDestPath()
	{
		if ((false === $this->_destPath) || !$this->Libs()->dir->Create($this->_destPath))
		{
			return false;
		}
		
		return $this->_destPath;
	}
	
	private function PrepareShellFileName($filename)
	{
		switch(PHP_OS)
		{
			case 'WINNT':	return str_replace('/', '\\', $filename);	
			default:	return str_replace('\\', '/', $filename);			
		}							
	}

	protected function RunShellCommand($mainCommand, $destFile, $code)
	{
		$sourceFile = $this->PrepareShellFileName($this->GetFilePath());
		$destFile = $this->PrepareShellFileName($destFile);
		$indexPath = $this->PrepareShellFileName(_ROOT_PATH. 'index.php');
		
		@chmod($this->GetFilePath(), _CHMOD_FILE);
		$commandFileName = $this->PrepareShellFileName(_ROOT_PATH. 'bat/' .$code. '.bat');
		$cd = "\n";
		
		switch(PHP_OS)
		{
			case 'WINNT':
						$command  = sprintf($mainCommand, '"' .$sourceFile. '"', '"' .$destFile. '"').$cd;		
						$command .= 'del "' .$sourceFile. '"'.$cd;
						$command .= 'php "' .$indexPath. '" module=admin ctrl=media action=confirm_file code=' .$code.$cd;
						$command .= 'del "' .$commandFileName. '"';	
						
						$taskCommand = 'at ' .date('H:i', time()+60). ' /NEXT: "' .$commandFileName. '"';
						break;
			default:
						$command = '#!/bin/sh'.$cd;
						$command .= sprintf($mainCommand, $sourceFile, $destFile). ' < /dev/null' .$cd;		
						$command .= 'chmod 777 ' .$destFile.$cd;
						$command .= 'unlink ' .$sourceFile.$cd;
						$command .= 'php ' .$indexPath. ' module=admin ctrl=media action=confirm_file code=' .$code.$cd;
						$command .= 'unlink ' .$commandFileName;

						$taskCommand = 'at -q b -f ' .$commandFileName. ' now';
		}						

		$f = @fopen($commandFileName, "wt");
		if (!$f) return false;
		
		fwrite($f, $command);
		fclose($f);
		@chmod($commandFileName, _CHMOD_FILE);	
			
		shell_exec($taskCommand);
		
		return true;		
	}	
	
	abstract protected function InitFile();
	abstract public function CreateThumbNail();
	abstract public function Convert($code);
}